<script type="text/javascript">

function show_confirm(name, id)
{
var r=confirm("Press a button!");
if (r==true)
  {
	alert("Are you sure you want to delete the item "+name+" ?");
  }
else
  {
	alert("The item "+name+" has not been deleted!");
  }
}
</script>

<style type="text/css">
table
{
padding:10px;
margin:50px;
background-color:#D8D8D8;
border:1px black;
outline-style:solid;
outline-width:thin;

}

#table_head
{
padding:10px;
margin:50px;
background-color:#B8B8B8;
border:1px solid red;
outline-style:solid;
outline-width:thin;

}
#description
{
color:#000000;
background-color:#e5eecc;
padding-top:8px;
padding-bottom:8px;
padding-left:10px;
padding-right:10px;
border:1px solid #d4d4d4;
background-image:url('images/bgfadegreen.gif');
background-repeat:repeat-x;
}

#search_item
{
padding:10px;
margin:50px;
background-color:#EFEFFB;
border:1px solid red;
outline-style:solid;
outline-width:thin;
}

#grey
{
padding:10px;
background-color:#E8E8E8;
}

</style>

<html>
	<head>
		<title>Admin page</title>
	</head>

	<body>
		<div id= "search_item">

	<center>
			<?php
				$data_item_name = array(
				  'name'        => 'item_name',
				  'id'          => 'item_name',
				  'value'       => $search_item_name,
				  'maxlength'   => '100',
				  'size'        => '50',
				  'style'       => 'width:50%',
				);
				$data_category_name = array(
				  'name'        => 'category_title',
				  'id'          => 'category_title',
				  'value'       => $search_category_title,
				  'maxlength'   => '100',
				  'size'        => '50',
				  'style'       => 'width:50%',
				);
			
				echo "Search for the item before you add it to the list<br />\n";
				echo form_open('admin_upload');
			?>
			
			<table  frame="above">
				<tr>
					<td align=right><?php	echo "Item name : "; ?></td>
					<td><?php	echo form_input($data_item_name); ?></td>
				</tr>
				<tr>
					<td align=right><?php	echo "Category name : "; ?></td>
					<td><?php	echo form_input($data_category_name); ?></td>
				</tr>
				<tr>
					<td align=right><?php	echo "OR ";  ?></td>
				</tr>
				<tr>
					<td align=right><?php	echo "Category : ";  ?></td>
					<td><?php	echo form_dropdown('category_page', $options);  ?></td>
				</tr>
				<tr>
					<td align=right><?php	echo form_submit('search', 'Search Item!');  ?></td>
				</tr>
				
			</table>
				
				<?php
				
				echo form_close();

				if (isset($item_search_result) && count($item_search_result)) 
				{
					echo "<br />\nTotal search result of the item requested: ".count($item_search_result)."<br />\n";

						?>
						<table cellspacing="0" cellpadding="0" width="85%" class="reference">
						  <tbody>

						  <tr id="table_head">
							<th align="left" width="5%" valign="top">Item ID</th>
							<th align="left" width="10%" valign="top">Item Name</th>
							<th align="left" width="10%" valign="top">Category</th>
							<th align="left" width="50%" valign="top">Description</th>
							<th align="left" width="5%" valign="top">Edit</th>
							<th align="left" width="5%" valign="top">Delete</th>
						  </tr>

						<?php  
							$i=0;

							foreach($item_search_result as $item)
							{
								echo form_open('admin_upload');
								
								if($i%2==0)
								{
							?>		<tr id="grey">
							<?php
								}
								else
								{
							?>		<tr>
							<?php
								}
							?>
						  
							<td valign="top"><?php echo $item->item_id; ?></td>
							<td valign="top"><?php echo $item->item_name; ?></td>
							<td valign="top"><?php echo $item->category; ?></td>
							<td valign="top"><?php echo $item->description; ?></td>
							<td valign="top"><?php echo form_submit('edit_item', 'Edit'); ?></td>

<!--						<td valign="top"><?php echo form_submit('delete_item', 'Delete'); ?></td>  -->
<!--							<td valign="top"><input type="submit" onclick="show_confirm($item->item_name, $item->item_id)" name="delete_item" value="Delete" /></td>  -->
<td valign="top"><img src="<?php echo $images_dir_url."btn_click_here.gif"; ?>" title="Delete this item" style="cursor:pointer" onclick="if(confirm('Are you sure you want to delete the item <?php echo $item->item_name; ?> ?')) document.location = '<?php echo base_url()."admin_upload/delete_item/".$item->item_id; ?>';"></td>  
								
						<?php
								echo form_hidden('hidden_item_id', $item->item_id); 
								echo form_close();
								$i++;
							}
						?>
						  </tr>
						  
						  </tbody>
						  </table>

			<?php 
				}
				if(isset($item_added) && count($item_added)) 
				{
//					echo "<br />\nItem ????".$item_added." has been succesfully added!: <br />\n";
				}
				else 
				{
					echo "<br />\nIf the item you're looking for isn't listed, go here: ";

					if(!$is_load_item)
					{ ?>
						<a href = "<?php echo base_url()."admin_upload/load_add_item/"; ?>"> Add Item</a>
					<?php
					}
					else
					{
						echo form_open_multipart('admin_upload');
						echo "<br>\n<br>\nChoose a category from the dropdown:".form_dropdown('category_page', $options); 
						echo "<br>\nEnter the name of the item: ".form_input('new_item'); 
						echo form_hidden('hidden_item_id',0);
						echo "<br>\n".form_submit('submit_item', 'Submit Item!');
						echo form_close();
					}
				}
			?>
		</center>

		<div id= "edit_item">
			<?php 
				if(isset($edit_item_id) && count($edit_item_id))
				{
			?>
						
		<div id= "images">
				<?php 
					if (isset($item_images) && count($item_images)) 
					{
						// the one with 2d imge array
						foreach($item_images as $image)
						{
				?>
							<div class="thumb">
								<a href =  "<?php echo $images_dir_url.$image->filename; ?>" > 
									<img src="<?php echo $images_dir_url.'thumbs/'.$image->filename; ?>" >  
								</a>
									<?php 
										
										// Edit caption form
										echo form_open('admin_upload');
										echo form_input('caption_here',$image->caption);
										echo form_hidden('hidden_image_id', $image->image_id);
										echo form_hidden('hidden_item_id', $image->item_id); 
										echo form_submit('edit_caption', 'Edit Caption');
										echo form_close();

										//Delete image
										echo form_open('admin_upload');    //?? why does this need to be admin_upload only?
										echo form_hidden('hidden_image_id', $image->image_id);
										echo form_hidden('hidden_item_id', $image->item_id); 
//										echo form_submit('delete_image', 'Delete!');
									?>
										<img src="<?php echo $images_dir_url."btn_click_here.gif"; ?>" title="Delete this image" style="cursor:pointer" onclick="if(confirm('Are you sure you want to delete the item <?php echo $image->filename; ?> ?')) document.location = '<?php echo base_url()."admin_upload/delete_image/".$image->image_id; ?>';">
									<?php
										echo form_close();
									?>
							</div>	
							
				<?php 		 
						}
					}
					else
					{ echo "<br>\n";
				?> 
						<div id="no_images"></div>    
			<?php	} ?>       
		</div>
				<?php
					echo "<br>\n<br>\nUpload a picture into the dropdown page<br>\n";
					echo form_open_multipart('admin_upload');
					echo form_upload('userfile');
					echo form_hidden('hidden_item_id', $edit_item_id); 
					echo form_submit('upload', 'Upload Image');

					echo "<br>\n<br>\nWrite a description about the item<br>\n";
					echo "Heading     : ".form_input('title', $description_heading)."<br />\n<br />\n";
					
					$description_detail = array(
					  'name'        => 'description',
					  'id'          => 'description',
					  'value'       => 'Fill in the variable from database later!',
					  'maxlength'   => '1000',
					  'size'        => '50',
					  'style'       => 'width:50%'
					);

					echo "Description : ".form_textarea('description', $item_description)."<br />\n";
					echo form_submit('submit_description', 'Submit!');
					echo form_close();
				}
			?>
		</div>
		</div>
	</body>

</html>